//提示框封装
const weToast = require('../../utils/weToast/weToast.js');
//图表封装
var wxCharts = require('../../utils/wxcharts-min.js');
//网络请求封装
var WxRequest = require('../../utils/webRequest.js');
var app = getApp();
var lineChart = null;
/* 图表 */
var bmiData = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var bloodHighData = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
var bloodLowData = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2];
var bloodSugarData = [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3];
var urineData = [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4];
var weightData = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5];
var time = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'];
var datas = [{
    name: 'BMI指数',
    data: bmiData,
    format: function(val, name) {
      return val;
    },
  },
  {
    name: '血压高压',
    data: bloodHighData,
    format: function(val, name) {
      return val;
    },
  },
  {
    name: '血压低压',
    data: bloodLowData,
    format: function(val, name) {
      return val;
    },
  },
  {
    name: '血糖',
    data: bloodSugarData,
    format: function(val, name) {
      return val;
    },
  },
  {
    name: '尿量',
    data: urineData,
    format: function(val, name) {
      return val;
    },
  },
  {
    name: '体重',
    data: weightData,
    format: function(val, name) {
      return val;
    },
  },
];
/*  */
Page({
  data: {
    currDate: '2018-08-05',
    startDate: '',
    endDate: '',
    dataLength: 0,
    navbar: ['BMI指数', '更多指数'],
    currentTab: 0,
    avatarUrl: "",
    currBMI: 23,
    weightTips: '555555455555555555555555555555555555555555555555555555555555555555555555',
    typeArray: ['全部', 'BMI', '血压高压', '血压低压', '血糖', '尿量', '体重'],
    typeArrayIndex: 0,
    isHiddenPicker: true,
    bmiData : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    bloodHighData : [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    bloodLowData : [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
    bloodSugarData :[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
    urineData : [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
    weightData : [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
    time : ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'],
    isHiddenChart:false,
  },

  onLoad: function() {
    
    var that = this;
    //console.log("重新加载time:" + time.length+","+bmiData)
    var date = this.getNowFormatDate();
    this.setData({
      currDate: date,
      startDate: date,
      endDate: date
    })
    wx.setNavigationBarTitle({
      title: app.globalData.Titles[2] //页面标题为路由参数
    })
    //画折线图
    this.setData({
      //必不可少，保证可滑动
      dataLength: time.length
    })
    that.dataQuery();
    //画折线图
    
    /*
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    
    if (windowWidth < time.length * 70) {
      windowWidth = time.length * 70
    }
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: time,
      animation: true,
      // background: '#f5f5f5',
      series: datas,
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '指数',
        format: function(val) {
          return val;
        },
      },
      width: windowWidth,
      height: 300,
      dataLabel: true,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });*/
  },
  tapName: function(event) {
    if (event.currentTarget.id == "blood_high") {

    }
  },
  scroll: function(e) {
    //console.log(e);
    var scrollLeft = e.detail.scrollLeft;
    this.setData({
      scrollLeft: scrollLeft
    })
  },
  //开始时间
  bindDateChange: function(e) {
    // console.log(e.detail.value)
    var that = this;
    that.setData({
      startDate: e.detail.value
    })
  },
  //结束时间
  bindDateChange2: function(e) {
    //console.log(e.detail.value)
    var that = this;
    that.setData({
      endDate: e.detail.value
    })
  },
  //获得当前时间
  getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate;
  },
  //查询功能
  dataQuery() {
    //console.log(this.getCategoryAndSeries());
    this.getHealthData();
    
  },
  //下拉菜单
  queryTypePickerChange: function(e) {
    var that = this;
    that.setData({
      typeArrayIndex: e.detail.value
    })
    that.reDrawCharts();
  },
  //重画折线图
  reDrawCharts: function() {
    var that = this;
    var series = that.getSeries();
    var categories = time;
    //console.log(time)
    if(time.length==0||time==null){
      that.setData({
        isHiddenChart : true,
      })
      categories=[0];
      var toast = new weToast(that);
      toast.warning('无记录');
      series = [{
        name: '',
        data: [0],
        format: function (val, name) {
          return val;
        },
      }]
    }else{
      that.setData({
        isHiddenChart: false,
      })
    }

      
    /*var that = this;
    var series = that.getSeries();
    var categories = time;*/
    /*this.setData({
      dataLength: categories.length
    })*/
    var windowWidth = 0;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    if (windowWidth < categories.length * 70) {
      windowWidth = categories.length * 70
    }
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: categories,
      animation: true,
      // background: '#f5f5f5',
      series: series,
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '指数',
        format: function(val) {
          return val;
        },
      },
      width: windowWidth,
      height: 300,
      dataLabel: true,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  //网络请求获取指数数据
  getHealthData: function() {
    var that = this;
    var postData = {
      uid: app.globalData.USERID,
      startDate: that.data.startDate,
      endDate: that.data.startDate
    }
    var url = '/diary/healthData'
    WxRequest.post(url, postData, this.getHealthDataSuccess, this.getHealthDataFail)
    //console.log(postData)

  },
  //获取数据成功的回调函数
  getHealthDataSuccess(res) {
    bmiData = res.data.BMI;
    bloodHighData = res.data.BPHigh;
    bloodLowData = res.data.BPLow
    bloodSugarData = res.data.GLS;
    weightData = res.data.weight
    urineData = res.data.UPD;
    time = res.data.date;
    //console.log(res);
    //this.bindDateChange
    var that = this;
    that.reDrawCharts();
    /*
    var that = this;
    var series = that.getSeries();
    that.reDrawCharts(time, series);*/
  },
  //获取数据失败的回调函数
  getHealthDataFail(res) {
    //console.log(res)
  },
  //生成series
  getSeries: function() {
    var that = this;
    var array = that.data.typeArray;
    var series = []
    switch (that.data.typeArrayIndex) {
      case "1":
        series.push(that.getSeriesItem(array[1], bmiData));
        break;
      case "2":
        series.push(that.getSeriesItem(array[2], bloodHighData));
        break;
      case "3":
        series.push(that.getSeriesItem(array[3], bloodLowData));
        break;
      case "4":
        series.push(that.getSeriesItem(array[4], bloodSugarData));
        break;
      case "5":
        series.push(that.getSeriesItem(array[5], urineData));
        break;
      case "6":
        series.push(that.getSeriesItem(array[6], weightData));
        break;
      default:
        series.push(that.getSeriesItem(array[1], bmiData));
        series.push(that.getSeriesItem(array[2], bloodHighData));
        series.push(that.getSeriesItem(array[3], bloodLowData));
        series.push(that.getSeriesItem(array[4], bloodSugarData));
        series.push(that.getSeriesItem(array[5], urineData));
        series.push(that.getSeriesItem(array[6], weightData));
        break;
    }

    return series;
  },
  //生成series元素
  getSeriesItem: function(name, data) {
    var series = {
      name: name,
      data: data,
    }
    return series;
  }

})